package com.basic.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import com.basic.service.IBaseService;
import com.common.util.CopyEntity;
import com.entity.concat.ITimeEntity;
import com.translate.mapper.IBaseMapper;

/**
 * 
 * @param <S> 时间类
 * @param <T> 实体类
 */
public abstract class BaseServiceImpl<S, T extends ITimeEntity<S>> implements IBaseService<T> {

	protected IBaseMapper<T> baseMapper;
	
	protected void insertProperties(T data) {
		
		Long time = System.currentTimeMillis() /1000;
		
		data.setCreateTime( time);
		
		data.setUpdateTime(time);
	}
	
	public long maxId() {
		return baseMapper.maxId();
	}
	
	@Override
	public long minId() {
		// TODO Auto-generated method stub
		return baseMapper.minId();
	}
	
	
	protected void updateProperties(T data) {
		
		Long time = System.currentTimeMillis() /1000;
		
		data.setUpdateTime( time);
	}
	public BaseServiceImpl(IBaseMapper<T> baseMapper) {
		this.baseMapper = baseMapper;
	}
	
	
	
	

	@Override
	public int count() {
		// TODO Auto-generated method stub
		return this.baseMapper.count();
	}

	/**
	 * 获取列表并缓存
	 * @param redisTemplate
	 * @param cacheKey
	 * @return
	 */
	protected List<T> listToCache(RedisTemplate<String, List<T>> redisTemplate, String cacheKey, int limitNumber) {
		
		ValueOperations<String, List<T>> opsForValue = redisTemplate.opsForValue();
		
		List<T> list = opsForValue.get(cacheKey);
		
		if (null != list) {
			return list;
		}
		
		list = this.getListByLimit(limitNumber);
		
		if (0 == list.size()) {
			opsForValue.set(cacheKey, list, 3, TimeUnit.SECONDS);
			return list;
		}
		
		opsForValue.set(cacheKey, list, 6, TimeUnit.SECONDS);

		return list;
	}
	
	/**
	 * 无参数查询
	 * @return
	 */
	public List<T> getListByLimit(int num) {
		
		return this.getBaseMapper().getDataByListByLimit(num);
	}
	
	
	
	
	
	@Override
	public <VO> int batchInsert(List<VO> data) {
		return parseBatchInsert(data);
	}

	private <VO> int parseBatchInsert(List<VO> data) {
		// TODO Auto-generated method stub
		
		int size = data.size();
		T[] orders = this.getEntityObject(size);
		
		Class<T> entityClass = this.getEntityClass();
		
		T entity;
		
		for (int i = 0; i < size; i++) {
			entity = CopyEntity.copyEntity(data.get(i), entityClass);
			
			afterParseInsert(entity);
			
			orders[i] = entity;
		}
		
		return this.baseMapper.batchInsert(orders);
	}
	
	    /**
     * 后置处理
     */
	protected void afterParseBatchInsert(List<T> data, int id) {
        
    }
	
	 /**
     * 批量添加
     * @param <VO>
     * @param data
     * @param target
     * @return
     */
    public int batchInsert(List<T> data, int id) {
    	this.afterParseBatchInsert(data, id);
    	return parseBatchInsert(data);
    }
	
	
	/**
	 * 后置处理
	 */
	protected void afterParseInsert(T data) {
		
	}
	
	
	
	
	
	@Override
	public T selectById(Integer id) {
		// TODO Auto-generated method stub
		return this.baseMapper.selectById(id);
	}

	@Override
	public List<T> selectList(T manuscript) {
		// TODO Auto-generated method stub
		return this.baseMapper.selectList(manuscript);
	}

	@Override
	public int insert(T manuscript) {
		// TODO Auto-generated method stub
		return this.baseMapper.insert(manuscript);
	}

	@Override
	public int update(T manuscript) {
		// TODO Auto-generated method stub
		return this.baseMapper.update(manuscript);
	}

	@Override
	public int deleteByIds(Integer[] ids) {
		// TODO Auto-generated method stub
		return this.baseMapper.deleteByIds(ids);
	}

	@Override
	public int deleteById(Integer id) {
		// TODO Auto-generated method stub
		return this.baseMapper.deleteById(id);
	}

	protected T[] getEntityObject(int size) {
		throw new Error("请在子类中实现getEntityClass");
	}
	
	protected Class<T> getEntityClass() {
		throw new Error("请在子类中实现getEntityClass");
	}
	

	@Override
	public <VO> int updatePayStatus(VO payManager) {
		// TODO Auto-generated method stub
		return this.baseMapper.updatePayStatus(payManager);
	}

	@Override
	public List<T> selectListById(int id) {
		// TODO Auto-generated method stub
		return this.baseMapper.selectListById(id);
	}
	
	@Override
	public <VO> List<VO> getOrderLogVOSById(int id, Class<VO> voClass) {
		
		List<T> selectListById = this.selectListById(id);
		
		if (0 == selectListById.size()) {
			return new ArrayList<>(0);
		}
		
		List<VO> list = new ArrayList<VO>();
		
		for (T t : selectListById) {
			list.add(CopyEntity.copyEntity(t, voClass));
		}
		
		return list;
	}
	
	@Override
	public List<T> selectListByIds(int[] ids) {
		// TODO Auto-generated method stub
		
		if (0 == ids.length) {
            return new ArrayList<>(0);
        }
		
		return baseMapper.selectListByIds(ids);
	}

	@SuppressWarnings("unchecked")
	protected <S extends IBaseMapper<T>> S getBaseMapper() {
		return (S) baseMapper;
	}
	
}
